﻿@using HCMS.Dictionary;
@using HCMS.Extensions
@model HCMS.Entities.Base.Notice
@{
    Layout = "~/Views/Shared/_Layout_Edit_Pear_Layui.cshtml";
}

<div class="layui-body layui-bg-gray" style="left:0;">
    <div class="layui-card">
        <form class="layui-form">
            <input type="hidden" asp-for="Id" />

            <div class="layui-tab layui-tab-card">
                <ul class="layui-tab-title">
                    <li class="layui-this">基础信息</li>
                    <li>其它</li>
                </ul>

                <div class="layui-tab-content">
                    <!-- 基础信息 -->
                    <div class="layui-tab-item layui-show">

                        <div class="layui-form-item">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-inline" style="width:250px;">
                                <input type="text" asp-for="Title" lay-verify="required" autocomplete="off" placeholder="请填写标题"
                                       class="layui-input" />
                            </div>

                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline" style="width:140px;">
                                <select asp-for="Status" asp-items="new DictCommon().StatusItems()"></select>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">内容</label>
                            <div class="layui-input-inline" style="width:90%;">
                                <textarea type="text" asp-for="Content" lay-verify="required" autocomplete="off" placeholder="请填写 内容"
                                          class="layui-textarea"></textarea>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">类型</label>
                            <div class="layui-input-inline" style="width:250px;">
                                <div class="layui-form">
                                    @{
                                        var typeList = new DictCommon().NoticeTypeItems();
                                        for (int i = 0; i < typeList.Count(); i++)
                                        {
                                            var item = typeList.ToList()[i];
                                            var isChecked = false;
                                            if (Model.TypeId == item.Value.ToInt(0))
                                            {
                                                isChecked = true;
                                            }

                                            <input type="radio" name="TypeId" value="@item.Value" title="@item.Text" @(isChecked ? "checked" : "")>
                                        }
                                    }
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">过期 时间</label>
                            <div class="layui-input-inline" style="width:250px;">
                                <input type="text" asp-for="ExpiresTime" class="layui-input" placeholder="yyyy-MM-dd HH:mm:ss" readonly>
                            </div>
                        </div>

                    </div>

                    <!-- 其它 -->
                    <div class="layui-tab-item">
                        @if (Model.Id > 0)
                        {
                            <div class="layui-form-item">
                                <div class="layui-form-label">创建时间</div>
                                <div class="layui-form-label content-label">
                                    @Model.CreateTime @Model.CreateUser
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-form-label">更新时间</div>
                                <div class="layui-form-label content-label">
                                    @Model.UpdateTime @Model.UpdateUser
                                </div>
                            </div>
                        }
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-fixbar btn-fixbar-box">
                <div class="layui-input-block">
                    <vc:menu-submit action-names="@(new string[]{"save"})"></vc:menu-submit>
                    <button type="button" class="layui-btn layui-btn-primary layui-border-red" lay-on="close">
                        <i class="layui-icon layui-icon-close"></i>关闭
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

@section Scripts {
    <!-- 富文本编辑器 -->
    <script src="~/tinymce/js/tinymce/tinymce.min.js" referrerpolicy="origin"></script>
    <script>
        tinymce.init({
          selector: 'textarea#Content',
          width: '90%',
          language: 'zh_CN',    // 语言
          license_key: 'gpl',   // 授权方式
          promotion: false,     // 禁止显示自带的【促销按钮】
          branding: false,      // 禁用状态栏中显示的“Powered by Tiny”链接
          setup: function(editor){
             // 设置字段内容
             editor.on('change', function () {
                document.getElementById('Content').value = editor.getContent();
             });
          }
        });
    </script>

    <script>
        layui.use(['form', 'jquery', 'laydate'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            var layer = layui.layer;
            var laydate = layui.laydate;

            datetimeRender();
            submitFn();

            // 时间选择
            function datetimeRender(){
                laydate.render({
                  elem: '#ExpiresTime',
                  type: 'datetime',
                  format: 'yyyy/MM/dd HH:mm:ss'
                });
            }

            // 表单提交
            function submitFn() {
                form.on('submit(save)', function (data) {

                    var layerIndex = layer.msg('正在保存', {
                      icon: 16,
                      shade: 0.1,
                      time: 0
                    });

                    $.ajax({
                        url: '/notice/save/',
                        type: 'post',
                        dataType: 'json',
                        data: data.field,
                        success: function (result) {
                            if (result.code === 200) {
                                layer.msg(result.message, { icon: 1, time: 1000 }, function () {
                                    parent.layer.close(parent.layer.getFrameIndex(window.name)); // 关闭当前页
                                    parent.layui.table.reload("tableList", {
                                        page: { curr: 1 },
                                    });
                                });
                            } else {
                                parent.layer.msg(result.message, { icon: 2 });
                            }
                        },
                        complete‌: function(){
                            layer.close(layerIndex);
                        }
                    });

                    return false;
                });
            }
        });
    </script>
}